package zelix.hack.hacks.xray;

import zelix.hack.hacks.xray.gui.GuiOverlay;
import zelix.hack.hacks.xray.keybinding.InputEvent;
import zelix.hack.hacks.xray.keybinding.KeyBindings;
import zelix.hack.hacks.xray.reference.Reference;
import zelix.hack.hacks.xray.reference.block.BlockData;
import zelix.hack.hacks.xray.reference.block.SimpleBlockData;
import zelix.hack.hacks.xray.store.BlockStore;
import zelix.hack.hacks.xray.store.GameBlockStore;
import zelix.hack.hacks.xray.store.JsonStore;
import zelix.hack.hacks.xray.xray.Controller;
import zelix.hack.hacks.xray.xray.Events;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import zelix.utils.hooks.visual.ChatUtils;

import java.util.HashMap;
import java.util.List;

import static zelix.hack.hacks.xray.AntiAntiXrayMod.isFirst;

public class XRay
{
	// This contains all of the games blocks to allow us to reference them
	// when needed. This allows us to avoid continually rebuilding
	public static GameBlockStore gameBlockStore = new GameBlockStore();

	public static Minecraft mc = Minecraft.getMinecraft();
	public static JsonStore blockStore = new JsonStore();

	//public static Logger logger;

	public static XRay instance;

	public InputEvent InputEvent;
	public Events Events;
	public GuiOverlay GuiOverlay;

	public void onEnable(){

		ChatUtils.message("Loading Anti-AntiXray");

		if (isFirst){
			KeyBindings.setup();
		}


		MinecraftForge.EVENT_BUS.register(InputEvent =  new InputEvent() );
		MinecraftForge.EVENT_BUS.register(Events =  new Events() );
		MinecraftForge.EVENT_BUS.register(GuiOverlay = new GuiOverlay() );

		MinecraftForge.EVENT_BUS.register( this );

		List<SimpleBlockData> data = blockStore.read();
		if( data.isEmpty() )
			return;

		HashMap<String, BlockData> map = BlockStore.getFromSimpleBlockList(data);
		Controller.getBlockStore().setStore(map);

		gameBlockStore.populate();

		instance = this;

		//EXIT
		//Controller.shutdownExecutor();
	}
	public void onDisable(){
		MinecraftForge.EVENT_BUS.unregister(InputEvent);
		MinecraftForge.EVENT_BUS.unregister(Events);
		MinecraftForge.EVENT_BUS.unregister(GuiOverlay);
		MinecraftForge.EVENT_BUS.unregister( this );
		Controller.shutdownExecutor();
		instance = null;
	}

	@SubscribeEvent
	public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
	{
		if (event.getModID().equals(Reference.MOD_ID))
		{
			ConfigManager.sync(Reference.MOD_ID, Config.Type.INSTANCE);
		}
	}
}
